#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
持续运行爬虫脚本
"""

import subprocess
import time
import signal
import sys
import os
from datetime import datetime

class ContinuousSpiderRunner:
    def __init__(self, interval_seconds=30):
        self.interval_seconds = interval_seconds
        self.current_process = None
        self.running = True
        
        # 设置信号处理
        signal.signal(signal.SIGINT, self.signal_handler)
        signal.signal(signal.SIGTERM, self.signal_handler)
    
    def signal_handler(self, signum, frame):
        """处理退出信号"""
        print(f"\n收到退出信号 {signum}，正在停止爬虫...")
        self.running = False
        if self.current_process:
            self.current_process.terminate()
        sys.exit(0)
    
    def run_spider(self):
        """运行爬虫"""
        print(f"\n=== {datetime.now().strftime('%Y-%m-%d %H:%M:%S')} 开始运行爬虫 ===")
        
        try:
            # 运行爬虫
            self.current_process = subprocess.Popen(
                ['scrapy', 'crawl', 'cnhnb_realtime'],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                text=True
            )
            
            # 等待爬虫完成
            stdout, stderr = self.current_process.communicate()
            
            if self.current_process.returncode == 0:
                print("✓ 爬虫运行成功")
            else:
                print(f"✗ 爬虫运行失败，返回码: {self.current_process.returncode}")
                if stderr:
                    print(f"错误信息: {stderr}")
            
        except Exception as e:
            print(f"✗ 运行爬虫时出错: {e}")
        
        finally:
            self.current_process = None
    
    def run_continuous(self):
        """持续运行爬虫"""
        print(f"=== 持续爬虫启动 ===")
        print(f"爬取间隔: {self.interval_seconds} 秒")
        print(f"按 Ctrl+C 停止")
        
        while self.running:
            try:
                # 运行爬虫
                self.run_spider()
                
                if not self.running:
                    break
                
                # 等待下次运行
                print(f"\n等待 {self.interval_seconds} 秒后重新运行...")
                print(f"下次运行时间: {datetime.now().timestamp() + self.interval_seconds}")
                
                # 分秒倒计时
                for i in range(self.interval_seconds, 0, -1):
                    if not self.running:
                        break
                    if i % 10 == 0:
                        print(f"剩余 {i} 秒...")
                    time.sleep(1)
                
            except KeyboardInterrupt:
                print("\n收到中断信号，正在退出...")
                break
            except Exception as e:
                print(f"运行出错: {e}")
                time.sleep(10)  # 出错后等待10秒再重试
        
        print("=== 持续爬虫已停止 ===")

def main():
    import argparse
    
    parser = argparse.ArgumentParser(description='持续运行惠农网爬虫')
    parser.add_argument('-i', '--interval', type=int, default=30, 
                       help='爬取间隔（秒），默认30秒')
    
    args = parser.parse_args()
    
    runner = ContinuousSpiderRunner(args.interval)
    runner.run_continuous()

if __name__ == '__main__':
    main() 
